{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "3\n",
      "5\n",
      "7\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "list = [1, 3, 5, 7, 9]\n",
    "\n",
    "for x in list:\n",
    "    print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5050\n"
     ]
    }
   ],
   "source": [
    "total = 0\n",
    "num = 1\n",
    "\n",
    "while num <= 100:\n",
    "    total += num\n",
    "    num += 1\n",
    "    \n",
    "print(total)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5050\n"
     ]
    }
   ],
   "source": [
    "total = 0\n",
    "\n",
    "# 顾头不顾尾\n",
    "for x in range(1, 101):\n",
    "    total += x\n",
    "    \n",
    "print(total)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "函数是一段完成特定任务的可重复使用的代码块。它接收一些输入（称为参\n",
    "数），根据这些输入执行一些操作，并返回一些输出。函数的主要目的是将\n",
    "代码分解成小块，以便更容易维护和复用。\n",
    "\n",
    "def function_name(parameters):\n",
    "    \"\"\"\n",
    "    Docstring: An optional docstring to describe the\n",
    "    function.\n",
    "    \"\"\"\n",
    "    # Function body: statements that perform the task of\n",
    "    the function.\n",
    "    # The body can contain one or more return statements.\n",
    "    return [expression]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8\n",
      "18\n",
      "39\n",
      "18\n"
     ]
    }
   ],
   "source": [
    "def add(a, b):\n",
    "    return a + b\n",
    "\n",
    "rst = add(3, 5)\n",
    "print(rst)\n",
    "\n",
    "rst = add(7, 11)\n",
    "print(rst)\n",
    "\n",
    "print(add(19, 20))\n",
    "print(rst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, Lifeng!\n",
      "你好！\n",
      "===\n",
      "Hello, Tom!\n",
      "How are you!\n",
      "How are you!\n",
      "How are you!\n"
     ]
    }
   ],
   "source": [
    "# 函数有3个参数\n",
    "# times是一个缺省值参数 = 1，可以不输入，如果不输入就是 1\n",
    "def greet(name, message, times = 1):\n",
    "    print(f\"Hello, {name}!\")\n",
    "    \n",
    "    for i in range(times):\n",
    "        print(message)\n",
    "        \n",
    "greet(\"Lifeng\", \"你好！\", 1)\n",
    "print(\"===\")\n",
    "greet(\"Tom\", \"How are you!\", 3)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```python\n",
    "\n",
    "def greet(name, message, times=1):\n",
    "    print(f\"Hello, {name}, {message}\")\n",
    "    \n",
    "    for i in range(times):\n",
    "        print(message)\n",
    "        \n",
    "greet(\"Lifeng\", \"你好！\", 1)\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello, lifeng, 你好!\n",
      "你好!\n"
     ]
    }
   ],
   "source": [
    "def greet(name, message, times = 1):\n",
    "    print(f\"hello, {name}, {message}\")\n",
    "    \n",
    "    for i in range(times):\n",
    "        print(message)\n",
    "        \n",
    "greet(\"lifeng\", \"你好!\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "116\n"
     ]
    }
   ],
   "source": [
    "def sum(*args):\n",
    "    total = 0\n",
    "    \n",
    "    for arg in args:\n",
    "        total += arg\n",
    "        \n",
    "    return total\n",
    "\n",
    "rst = sum(2, 3, 5, 7, 11, 88)\n",
    "print(rst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "115\n"
     ]
    }
   ],
   "source": [
    "def sum(*args):\n",
    "    total=0\n",
    "    \n",
    "    for arg in args:\n",
    "        total +=arg\n",
    "        \n",
    "        \n",
    "    return total\n",
    "\n",
    "rst = sum(3,4,8,10,90)\n",
    "print(rst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name  =  Tod\n",
      "gender  =  male\n",
      "age  =  18\n"
     ]
    }
   ],
   "source": [
    "def print_kv(**kv):\n",
    "    for k, v in kv.items():\n",
    "        print(k, ' = ', v)\n",
    "    \n",
    "    \n",
    "a = {\n",
    "    \"name\": \"Tod\",\n",
    "    \"gender\": \"male\",\n",
    "    \"age\": 18\n",
    "}\n",
    "\n",
    "print_kv(**a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name = tod\n",
      "gender = male\n",
      "age = 18\n"
     ]
    }
   ],
   "source": [
    "def print_kv(**kv):\n",
    "    for k, v in kv.items():\n",
    "        print(k, \"=\",v)\n",
    "        \n",
    "a = {\n",
    "    \"name\":\"tod\",\n",
    "    \"gender\":\"male\",\n",
    "    \"age\":18\n",
    "}\n",
    "    \n",
    "print_kv(**a)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
